//
//  HLTBandcardViewController.m
//  huiliantong
//
//  Created by hlt on 15/9/15.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "HLTBandcardViewController.h"
#import "HLTyijianbangkaTableViewController.h"
#import "HLTzidingyibangkaTableViewController.h"
@interface HLTBandcardViewController ()<UIAlertViewDelegate>

@property (weak, nonatomic) IBOutlet UILabel *username;


@property (weak, nonatomic) IBOutlet UILabel *cardno;


@property (weak, nonatomic) IBOutlet UILabel *yue;



@property (weak, nonatomic) IBOutlet UIButton *zidongkaika;


@property (weak, nonatomic) IBOutlet UIButton *zidingyikaika;

@property BOOL type;









@end

@implementation HLTBandcardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //设置返回的按钮的字体的颜色的
    self.navigationController.navigationBar.tintColor=[UIColor whiteColor];
    //设置导航的背景颜色的
    self.navigationController.navigationBar.barTintColor = navigationBarColor;
    //    self.navigationController.title=@"登陆";
    [self.navigationItem setTitle:@"卡绑定"];
    //默认为yes
    self.type=YES;
    
    Account *acc=[[AccountTool sharedAccountTool]currentAccount];
    
   if ([acc.cardNo isEqual:@""])
    {
        
        self.type=YES;
    }
    else
    {
        self.type=NO;
    }
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.extendedLayoutIncludesOpaqueBars = NO;
    self.modalPresentationCapturesStatusBarAppearance = NO;
    
    
    
    //添加退出按钮
    
    UIButton *searchButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    searchButton.titleLabel.font = [UIFont systemFontOfSize: 13.0];
    [searchButton setTitle:@"退出" forState:UIControlStateNormal];
    [searchButton addTarget:self action:@selector(usertuichu) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc] initWithCustomView:searchButton];
    //     self.navigationItem.rightBarButtonItems = @[searchItem,moreItem];
    self.navigationItem.rightBarButtonItem = searchItem;
    
    
    
}



-(void) usertuichu
{
    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
    
    //退出登录
    [[AccountTool sharedAccountTool] tuichu:acc];
    
    [self viewWillDisappear:YES];
    [self viewWillAppear:YES];
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}




- (IBAction)yijiankaika:(id)sender {
    
    if (self.type) {
    
    UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                  message:@"你好，如果您已领取了汇联通卡，可以不需要本次操作，该操作会新开户一张普通卡，确定要办理一张普通卡吗？"
                                                 delegate:self
                                        cancelButtonTitle:@"取消"
                                        otherButtonTitles:@"好",nil];
    [alert show];
    
    }
    else{
        
        UIView *view1=[[UIView alloc] initWithFrame:self.view.bounds];
        view1.backgroundColor=[UIColor grayColor];
        view1.alpha=0.4;
        [self.view addSubview:view1];
        MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
        indicatorView.delegate = self;
        indicatorView.numberOfCircles = 3;
        indicatorView.radius = 20;
        indicatorView.internalSpacing = 3;
        indicatorView.center = self.view.center;
        [indicatorView startAnimating];
        [self.view addSubview:indicatorView];


        //这个是一键解绑的事件
        
        Account *acc=[[AccountTool sharedAccountTool] currentAccount];
        
        //一键解绑接口 (user_name=18285117540，num=18285117540，cardNum=卡号，queryType=‘S’)
        NSString *url=[yijianjiebang stringByAppendingFormat:@"&user_name=%@&num=%@&cardNum=%@&queryType=S",acc.custPhone,acc.custPhone,acc.cardNo];
        
        [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
            
//            NSLog(@"%@",ary);
            //将nsnumber 装换成nsstring
            //            NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
            //            NSString *str = [numberFormatter stringFromNumber:ary[@"code"]];
            //
            [indicatorView stopAnimating];
            [view1 removeFromSuperview];

            UIAlertView*alert = [[UIAlertView alloc]initWithTitle:ary[@"msg"]
                                                          message:nil
                                                         delegate:nil
                                                cancelButtonTitle:@"好"
                                                otherButtonTitles:nil,nil];
            [alert show];
            
            acc.cardNo=@"";
            self.type=YES;
            [self viewWillAppear:YES];
            [[AccountTool sharedAccountTool] addAccounts:acc];
            
        } faliure:^(NSString *error) {
            
            [indicatorView stopAnimating];
            [view1 removeFromSuperview];

        }];

        
        
    }
    
}




- (IBAction)zidingyikabangding:(id)sender {
    
    
    if (self.type) {
        
        [EYInputPopupView popViewWithTitle:@"请输入卡号(19位)" contentText:@"卡号"
                                      type:EYInputPopupView_Type_single_line_text
                               cancelBlock:^{
                                   
                               } confirmBlock:^(UIView *view, NSString *text) {
                                   
                                   
                                   
                                   UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                                   HLTzidingyibangkaTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTzidingyibangkaTableViewController"];
                                   vc.kahao=text;
                                   
                                   [self.navigationController pushViewController:vc animated:NO];
                                   
                               } dismissBlock:^{
                                   
                               }];

        
        
    }
    else
    {
        //在没有绑定卡得情况下执行的方法
        
        UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"请联系客服，客服电话：96696021"
                                                      message:nil
                                                     delegate:nil
                                            cancelButtonTitle:@"取消"
                                            otherButtonTitles:@"好",nil];
        [alert show];

        
        
        
        
        
    }

    
    
    
    
    
    
    
    
    
    
}




//uialert的委托方法
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex==1) {
        //点击了好，进入下一个页面
        
        UIView *view1=[[UIView alloc] initWithFrame:self.view.bounds];
        view1.backgroundColor=[UIColor grayColor];
        view1.alpha=0.4;
        [self.view addSubview:view1];
        MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
        indicatorView.delegate = self;
        indicatorView.numberOfCircles = 3;
        indicatorView.radius = 20;
        indicatorView.internalSpacing = 3;
        indicatorView.center = self.view.center;
        [indicatorView startAnimating];
        [self.view addSubview:indicatorView];
        
        
        
        Account *acc=[[AccountTool sharedAccountTool] currentAccount];
        
        NSString *url=[yijianbangka stringByAppendingFormat:@"&user_name=%@",acc.custPhone];
        
        [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
            
            [indicatorView stopAnimating];
            [view1 removeFromSuperview];
            
            //将nsnumber 装换成nsstring
            NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
            NSString *str = [numberFormatter stringFromNumber:ary[@"code"]];
            
            
            if ([str isEqual:@"2"]) {
                
                UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
                HLTyijianbangkaTableViewController *vc = [storyBoard instantiateViewControllerWithIdentifier:@"HLTyijianbangkaTableViewController"];
                
                vc.kahao=ary[@"CardNum"];
                vc.pwd1=ary[@"password"];
                vc.liushuihao1=ary[@"transaction_id"];
                [self.navigationController pushViewController:vc animated:NO];
            }
            else
            {
                UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"一键绑定卡失败~_~"
                                                              message:ary[@"msg"]
                                                             delegate:nil
                                                    cancelButtonTitle:@"好"
                                                    otherButtonTitles:nil,nil];
                [alert show];
                
            }
            
            
            
        } faliure:^(NSString *error) {
            
            [indicatorView stopAnimating];
            [view1 removeFromSuperview];
        }];
        
        
        
        
        
        
        
    }
}




- (void)viewWillAppear:(BOOL)animated
{
    Account *acc=[[AccountTool sharedAccountTool]currentAccount];
    self.username.text=acc.custPhone;
    
  
    if ([acc.cardNo isEqual:@""]) {
        self.cardno.text=@"未绑定汇联通卡 !";
        self.yue.text=@"未绑定无法完成商城支付";
        self.type=YES;
        //取消隐藏自定义开卡按钮
        self.zidingyikaika.hidden=NO;
        [self.zidingyikaika setTitle:@"自定义绑卡" forState:UIControlStateNormal];//设置button的title
        
        [self.zidongkaika setTitle:@"一键绑卡" forState:UIControlStateNormal];//设置button的title

        
    }
    else
    {
        //在有卡号的情况的下设置为no
        self.type=NO;
        self.cardno.text=[NSString stringWithFormat:@"卡号:%@",acc.cardNo];
        self.yue.text=[NSString stringWithFormat:@"余额:%@",acc.YKTCardBalance];
        //隐藏自定义开卡按钮
        self.zidingyikaika.hidden=YES;
        [self.zidingyikaika setTitle:@"申请补卡" forState:UIControlStateNormal];//设置button的title
        
        [self.zidongkaika setTitle:@"一键解绑汇联通卡" forState:UIControlStateNormal];//设置button的title
    }

    
}






@end
























